package com.lancoo.answer.view.xgSaResultView

import android.os.Handler
import android.view.View
import com.lancoo.answer.databinding.EvXgSaFragmentResultBinding
import com.lancoo.answer.model.ConstantBean
import com.lancoo.answer.model.bean.CognitiveBean
import com.lancoo.answer.util.LancooUtils
import com.lancoo.answer.util.ScoreSoundUtils
import com.lancoo.answer.util.setDinBoldTtf
import com.lancoo.answer.view.base.XBaseFragment
import com.lancoo.answer.view.dialog.XgDialogLevelUp
//import com.lancoo.answer.view.dialog.XgDialogLevelUp
import com.lancoo.answer.view.xgSaResultView.adapter.XgSaResultAnswerSheetParentAdapter
import com.lancoo.answer.widget.CountNumberView
import java.lang.Math.abs

class XgSaResultFragment() : XBaseFragment<EvXgSaFragmentResultBinding>() {

    companion object {
        fun loadFragment(): XgSaResultFragment {
            return XgSaResultFragment()
        }
    }

    private lateinit var cognitiveBean: CognitiveBean
    private var scoreSoundUtil: ScoreSoundUtils? = null

    override fun initView() {
        //初始化短音频
        scoreSoundUtil = ScoreSoundUtils(requireContext())
        scoreSoundUtil!!.init()

        cognitiveBean = ConstantBean.cognitiveBean!!

        showCorrectRateView()
        showScoreChange()
        showAnswerSheet()
    }

    //显示同类题作答正确率
    private fun showCorrectRateView(isShowAnim:Boolean = true) {
        var isShowCorrectRate = true//是否是有效正确率
        var correctRateAll = 0.0
        val sameQuesSize = cognitiveBean.TypeList!![0].QuesList?.size
        outLoop@ for (i in 0 until sameQuesSize!!) {
            val correctRate = cognitiveBean.TypeList!![0].QuesList!![i].ScoreRate
            if (correctRate == -1.0f) {
                isShowCorrectRate = false
                break@outLoop
            }
            correctRateAll += correctRate
        }
        if (isShowCorrectRate) {
            val scoreRate =
                LancooUtils.getCorrectRate(correctRateAll * 100, sameQuesSize.toDouble())
            val intScoreRate = scoreRate.toInt()
            //显示准确率
            if(isShowAnim) {
                binding.IncludeA.TvCorrectRate.showNumberWithAnimation(
                    intScoreRate.toFloat(),
                    CountNumberView.INTREGEX
                )
            }
            binding.IncludeA.TvCorrectRate.setDinBoldTtf()
            binding.IncludeA.TvSymbols.setDinBoldTtf()
        }
    }

    //显示认知分和预估成绩变化
    private fun showScoreChange() {
        val returnFlag = cognitiveBean.CognitionInfo!!.ReturnFlag
        binding.IncludeA.LlScoreChangeView.visibility = View.GONE
        binding.IncludeA.DotProgressBar.visibility = View.VISIBLE
        binding.IncludeA.DotProgressBar.SetDotRadius(16f)
        binding.IncludeA.DotProgressBar.showNow()

        if (returnFlag) {
            binding.IncludeA.LlScoreChangeView.visibility = View.VISIBLE
            binding.IncludeA.DotProgressBar.visibility = View.GONE
            binding.IncludeA.DotProgressBar.hideNow()

            val incrCognitiveIndex = cognitiveBean.CognitionInfo!!.IncrCognitiveIndex
            val incrEstimateScore = cognitiveBean.CognitionInfo!!.IncrEstimateScore
            binding.IncludeA.TvScoreTitle2.text = "${
                when (cognitiveBean.GlobalGrade) {
                    "K6", "K7", "K8", "K9" -> "中考"
                    else -> "高考"
                }
            }成绩"
            //动态显示我的认知分变化及预估成绩变化
            if (incrCognitiveIndex.contains("-")) {
                binding.IncludeA.TvScoreSymbols1.text = "↓"
            } else {
                binding.IncludeA.TvScoreSymbols1.text = "↑"
            }
            binding.IncludeA.TvScoreChange1.showNumberWithAnimation(
                abs(incrCognitiveIndex.toFloat()),
                CountNumberView.INTREGEX
            )
            binding.IncludeA.TvScoreChange1.setDinBoldTtf()

            if (incrEstimateScore.contains("-")) {
                binding.IncludeA.TvScoreSymbols2.text = "↓"
            } else {
                binding.IncludeA.TvScoreSymbols2.text = "↑"
            }
            binding.IncludeA.TvScoreChange2.showNumberWithAnimation(
                abs(incrEstimateScore.toFloat()),
                CountNumberView.INTREGEX
            )
            binding.IncludeA.TvScoreChange2.setDinBoldTtf()

            val currCognitiveGrade = cognitiveBean.CognitionInfo!!.CurrCognitiveGradeName
            val lastCognitiveGrade = cognitiveBean.CognitionInfo!!.LastCognitiveGradeName
            val lastIndex = LancooUtils.stringToAscii(lastCognitiveGrade).toInt()
            val currIndex = LancooUtils.stringToAscii(currCognitiveGrade).toInt()
            if (lastIndex != currIndex) {
                if (lastIndex < currIndex) {
                    scoreSoundUtil!!.playSound(3)
                } else if (lastIndex > currIndex) {
                    scoreSoundUtil!!.playSound(2)
                }
                showLevelUpDialog(lastIndex, currIndex)
            }
        }
    }

    //显示答题卡
    private fun showAnswerSheet(){
        //加载答题卡
        binding.IncludeB.RvResultParent.removeAllViews()
        val adapterParent = XgSaResultAnswerSheetParentAdapter(
            requireActivity(), cognitiveBean
        )
        adapterParent.setClickParentListener(object :
            XgSaResultAnswerSheetParentAdapter.OnClickParentListener {
            override fun onClick(sortIndex: String) {
//                JumpActivityHelp.jumpToRecommendAnalysisActivity(
//                    activity, sameQuesDetail, sortIndex, "同类题训练"
//                )
            }
        })
        binding.IncludeB.RvResultParent.adapter = adapterParent
    }

    override fun onDestroy() {
        super.onDestroy()
        scoreSoundUtil!!.stopAllSoundPool()
    }

    override fun onPause() {
        super.onPause()
        scoreSoundUtil!!.stopAllSoundPool()
    }

    private fun showLevelUpDialog(lastIndex: Int, currIndex: Int) {
        //webp动图
        Handler().postDelayed({
            val dialogLevelUp = XgDialogLevelUp(requireActivity(), lastIndex, currIndex)
            dialogLevelUp.show()
        }, 300)
    }

    fun refreshResultScoreView(mCognitiveBean: CognitiveBean) {
        cognitiveBean = mCognitiveBean
        showCorrectRateView(false)
    }

    fun refreshResultCognitiveView(mCognitiveBean: CognitiveBean) {
        cognitiveBean = mCognitiveBean
        showScoreChange()
    }

    fun refreshResultAnswerSheetView(mCognitiveBean: CognitiveBean) {
        cognitiveBean = mCognitiveBean
        showAnswerSheet()
    }

}